package cn.itcast.service.impl;

import cn.itcast.dao.WeiboDao;
import cn.itcast.domain.Weibo;
import cn.itcast.domain.WeiboComm;
import cn.itcast.service.WeiboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Date :17:34 2021/6/14
 * @Author :xuhe
 */
@Service("weiboService")
public class WeiboServiceImpl implements WeiboService {

    @Autowired
    private WeiboDao weiboDao;
    @Override
    public void publishWeibo(Weibo weibo) {
        weiboDao.publishWeibo(weibo);
    }

    @Override
    public List<Weibo> findAllWeibo() {
        List<Weibo> weiboList=weiboDao.findAllWeibo();
        return weiboList;
    }

    @Override
    public List<Weibo> findWeiboByUserid(String userid) {
        List<Weibo> weiboList=weiboDao.findWeiboByUserid(userid);
        return weiboList;
    }

    @Override
    public void deleteWeiboByid(String weiboid) {

    }

    @Override
    public int weiboComment(WeiboComm comm) {
        int commNum = 0;
        //comm.setCommentDate(nowDate());
        comm.setBeCommentid(weiboDao.findUserIdbyWId(comm.getWeiboid()));
        comm.setCommentLike(0); 	//默认点赞数为0
        //保存评论信息
        weiboDao.weiboComment(comm);
        Weibo weibo = weiboDao.findWeiboBywId(comm.getWeiboid());
        //查找微博评论数
        commNum = weiboDao.findCommNum(comm.getWeiboid());
        weibo.setComnum(commNum);
        //更新微博评论数目
        weiboDao.updateWeibo(weibo);


        return commNum;
    }

    @Override
    public String findUserIdbyWId(long wId) {
        String uid = weiboDao.findUserIdbyWId(wId);
        return uid;
    }

    @Override
    public Weibo findWeiboBywId(long weiboId) {
        Weibo weibo=weiboDao.findWeiboBywId(weiboId);
        return weibo;
    }

    @Override
    public int findCommNum(long weiboId) {
        int cnum=weiboDao.findCommNum(weiboId);
        return cnum;
    }

    @Override
    public void updateWeibo(Weibo weibo) {
        weiboDao.updateWeibo(weibo);
    }

    @Override
    public List<WeiboComm> finCommByWid(long weiboid) {
        List<WeiboComm> list=weiboDao.finCommByWid(weiboid);
        return list;
    }

    @Override
    public List<Weibo> search(String key) {
        List<Weibo> weiboList = weiboDao.search(key);
        return weiboList;
    }
}
